function getStyle(obj, attr) {
    if (obj.currentStyle) {
        return obj.currentStyle[attr];
    }
    else {
        return getComputedStyle(obj, false)[attr];
    }
}
function getClass(sClass, obj) {
    var aRr = [];
    if (obj) {
        var aTag = obj.getElementsByTagName('*');
    }
    else {
        var aTag = document.getElementsByTagName('*');
    }
    for (var i = 0; i < aTag.length; i++) {
        var aClass = aTag[i].className.split(" ");
        for (var j = 0; j < aClass.length; j++) {
            if (aClass[j] == sClass) {
                aRr.push(aTag[i]);
            }
        }
    }
    return aRr;
}

//碰撞+重力框架
function startMoveG(obj, iSpeedX, iSpeedY) {
    var t1 = document.body.scrollTop || document.documentElement.scrollTop;
    var l1 = document.body.scrollLeft || document.documentElement.scrollLeft;
    var x = 0;
    clearInterval(obj.timer);
    obj.timer = setInterval(function () {
        x += 1;
        document.title = iSpeedX;
        iSpeedY += 3;
        var l = obj.offsetLeft + iSpeedX;
        var t = obj.offsetTop + iSpeedY;

        if (t >= t1 + document.documentElement.clientHeight - obj.offsetHeight) {
            iSpeedY *= -0.8;
            iSpeedX *= 0.8;
            t = t1 + document.documentElement.clientHeight - obj.offsetHeight;
        } else if (t <= t1) {
            iSpeedY *= -1;
            iSpeedX *= 0.8;
            t = t1;
        }

        if (l >= l1 + document.documentElement.clientWidth - obj.offsetWidth) {
            iSpeedX *= -0.8;
            l = l1 + document.documentElement.clientWidth - obj.offsetWidth;
        } else if (l <= l1) {
            iSpeedX *= -0.8;
            l = l1;
        }

        if (Math.abs(iSpeedX) < 1) {
            iSpeedX = 0;
        }

        if (Math.abs(iSpeedY) < 1) {
            iSpeedY = 0;
        }

        obj.style.left = l + 'px';
        obj.style.top = t + 'px';

        if (obj.offsetTop == t1 + document.documentElement.clientHeight - obj.offsetHeight && iSpeedY == 0) {
            clearInterval(obj.timer);
        }
    }, 30);
}
//带摩擦的弹性运动
function startMoveE(obj, json, fn) {
    clearInterval(obj.timer);
    obj.iSpeed = {};
    for (var attr in json) {
        obj.iSpeed[attr] = 0;
    }
    obj.timer = setInterval(function () {
        var iEnd = true;
        var attr = "";
        for (attr in json) {
            var iNow = parseInt(getStyle(obj, attr));
            obj.iSpeed[attr] += (json[attr] - iNow) / 5;
            document.title = obj.iSpeed[attr];
            obj.iSpeed[attr] *= 0.7;
            if (Math.abs(obj.iSpeed[attr]) < 1 && Math.abs(iNow - json[attr]) < 1) {
                obj.style[attr] = json[attr] + 'px';
                continue;
            } else {
                iNow = Math.round(iNow + obj.iSpeed[attr]);
                obj.style[attr] = iNow + 'px';
                iEnd = false;
            }
        }
        if (iEnd) {
            clearInterval(obj.timer);
            fn && fn();
        }
    }, 30);
}
//拖拽框架
function drag(obj, bRun) {
    obj.onmousedown = function (ev) {
        var oEvent = ev || window.event;
        var disX = oEvent.clientX - obj.offsetLeft;
        var disY = oEvent.clientY - obj.offsetTop;
        var lastX = 0;
        var lastY = 0;
        var iX = 0;
        var iY = 0;
        document.onmousemove = function (ev) {
            var oEvent = ev || event;
            var l = oEvent.clientX - disX;//当前位置
            var t = oEvent.clientY - disY;

            var t1 = document.body.scrollTop || document.documentElement.scrollTop;
            var l1 = document.body.scrollLeft || document.documentElement.scrollLeft;
            if (t >= t1 + document.documentElement.clientHeight - obj.offsetHeight) {
                t = t1 + document.documentElement.clientHeight - obj.offsetHeight;
            } else if (t <= t1) {
                t = t1;
            }
            if (l >= l1 + document.documentElement.clientWidth - obj.offsetWidth) {
                l = l1 + document.documentElement.clientWidth - obj.offsetWidth;
            } else if (l <= l1) {
                l = l1;
            }
            obj.style.left = l + 'px';
            obj.style.top = t + 'px';
            //获取当前点的实时速度
            iX = l - lastX;
            iY = t - lastY;
            //然后需要把当前点赋值给参照点，给下一次计算使用
            lastX = l;
            lastY = t;
        };
        document.onmouseup = function () {
            document.onmousemove = null;
            document.onmouseup = null;
            bRun && startMoveG(obj, iX, iY);
        };
        clearInterval(obj.timer);
        return false;
    }
}